/**
 * Copyright (c) 2013-2022 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.connection;

import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.ReadMode;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.SubscriptionMode;

/**
 * ConnectionManager,单机模式
 */
public class SingleConnectionManager extends MasterSlaveConnectionManager {

    public SingleConnectionManager(SingleServerConfig cfg, ServiceManager serviceManager) {
        super(create(cfg), serviceManager);
    }

    private static MasterSlaveServersConfig create(SingleServerConfig cfg) {
        MasterSlaveServersConfig newconfig = new MasterSlaveServersConfig();
        // 决定了客户端发送ping消息的频率
        newconfig.setPingConnectionInterval(cfg.getPingConnectionInterval());
        newconfig.setSslEnableEndpointIdentification(cfg.isSslEnableEndpointIdentification());
        newconfig.setSslProvider(cfg.getSslProvider());
        newconfig.setSslTruststore(cfg.getSslTruststore());
        newconfig.setSslTruststorePassword(cfg.getSslTruststorePassword());
        newconfig.setSslKeystore(cfg.getSslKeystore());
        newconfig.setSslKeystorePassword(cfg.getSslKeystorePassword());
        newconfig.setSslProtocols(cfg.getSslProtocols());
        newconfig.setSslCiphers(cfg.getSslCiphers());
        newconfig.setSslKeyManagerFactory(cfg.getSslKeyManagerFactory());
        newconfig.setSslTrustManagerFactory(cfg.getSslTrustManagerFactory());

        // 连接失败时进行重试的次数
        newconfig.setRetryAttempts(cfg.getRetryAttempts());
        // 每次重试连接之间的等待时间
        newconfig.setRetryInterval(cfg.getRetryInterval());
        // 单个命令执行的超时时间, 关注的是单次命令交互的及时性
        newconfig.setTimeout(cfg.getTimeout());
        // 密码
        newconfig.setPassword(cfg.getPassword());
        // 用户名
        newconfig.setUsername(cfg.getUsername());
        // 选中的库名
        newconfig.setDatabase(cfg.getDatabase());
        // 连接名
        newconfig.setClientName(cfg.getClientName());
        // 主节点地址
        newconfig.setMasterAddress(cfg.getAddress());
        // 主节点连接池大小,默认值64
        newconfig.setMasterConnectionPoolSize(cfg.getConnectionPoolSize());
        // 单个连接最大订阅数量
        newconfig.setSubscriptionsPerConnection(cfg.getSubscriptionsPerConnection());
        // slave节点的最大订阅（pub/sub）连接池大小。
        newconfig.setSubscriptionConnectionPoolSize(cfg.getSubscriptionConnectionPoolSize());
        // 连接超时时间
        newconfig.setConnectTimeout(cfg.getConnectTimeout());
        // 连接没有任何执行命令下空闲超,关注的是连接资源的管理
        newconfig.setIdleConnectionTimeout(cfg.getIdleConnectionTimeout());
        // DNS监控的间隔时间
        newconfig.setDnsMonitoringInterval(cfg.getDnsMonitoringInterval());


        // 主节点连接最小空闲大小为24
        newconfig.setMasterConnectionMinimumIdleSize(cfg.getConnectionMinimumIdleSize());
        // slave节点中，每个节点的最小空闲订阅（pub/sub）连接数量。
        newconfig.setSubscriptionConnectionMinimumIdleSize(cfg.getSubscriptionConnectionMinimumIdleSize());
        // 默认的读取模式设置为从SLAVE读取
        newconfig.setReadMode(ReadMode.MASTER);
        // 订阅模式变量，默认为MASTER模式
        newconfig.setSubscriptionMode(SubscriptionMode.MASTER);
        // 连接的保活时间
        newconfig.setKeepAlive(cfg.isKeepAlive());
        newconfig.setTcpNoDelay(cfg.isTcpNoDelay());
        newconfig.setNameMapper(cfg.getNameMapper());
        // Redis连接的用户凭证信息
        newconfig.setCredentialsResolver(cfg.getCredentialsResolver());
        newconfig.setCommandMapper(cfg.getCommandMapper());

        return newconfig;
    }

}
